import { createRouter, createWebHashHistory } from 'vue-router'
import storage from '../utils/storage'
import { generateRoute } from './..//utils/date.js'
import API from '../api'
const modules = import.meta.glob('../views/*.vue')

const routes = [
  {
    name: 'home',
    path: '/',
    redirect: '/welcome',
    meta: {
      title: '首页'
    },
    component: () => import('@/components/Home.vue'),
    children: [
      {
        name: 'Welcome',
        path: '/welcome',
        component: () => import('@/views/Welcome.vue'),
        meta: {
          title: '欢迎页'
        }
      }
      // {
      //   name: 'Compile',
      //   path: '/work/compile/:id',
      //   component: () => import('@/views/Compile.vue'),
      //   meta: {
      //     title: '文档编辑'
      //   }
      // }
      // {
      //   name: 'User',
      //   path: '/system/user',
      //   component: () => import('@/views/User.vue'),
      //   meta: {
      //     title: '用户管理'
      //   }
      // },
      // {
      //   name: 'Menu',
      //   path: '/system/menu',
      //   component: () => import('@/views/Menu.vue'),
      //   meta: {
      //     title: '菜单管理'
      //   }
      // },
      // {
      //   name: 'Role',
      //   path: '/system/role',
      //   component: () => import('@/views/Role.vue'),
      //   meta: {
      //     title: '角色管理'
      //   }
      // },
      // {
      //   name: 'Dept',
      //   path: '/system/dept',
      //   component: () => import('@/views/Dept.vue'),
      //   meta: {
      //     title: '部门管理'
      //   }
      // },
      // {
      //   name: 'Item',
      //   path: '/project/item',
      //   component: () => import('@/views/Item.vue'),
      //   meta: {
      //     title: '项目库'
      //   }
      // },
      // {
      //   name: 'Case',
      //   path: '/project/case',
      //   component: () => import('@/views/Case/index.vue'),
      //   meta: {
      //     title: '案例库'
      //   }
      // }
    ]
  },
  {
    name: 'Login',
    path: '/login',
    meta: {
      title: '登录'
    },
    component: () => import('@/views/Login.vue')
  },
  {
    name: '404',
    path: '/404',
    meta: {
      title: '页面不存在'
    },
    component: () => import('@/views/404.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 根据菜单列表动态渲染 router
async function loadAsyncRoutes() {
  let userInfo = storage.getItem('userInfo') || {}
  if (userInfo.token) {
    try {
      const menuList = await API.getMenuList()
      let routes = generateRoute(menuList)
      routes.map(route => {
        let url = route.component
        for (const path in modules) {
          if (path.indexOf(url) !== -1) {
            route.component = modules[path]
            router.addRoute('home', route)
          }
        }
      })
    } catch (error) {
      console.error(error)
    }
  }
}

await loadAsyncRoutes()
// 判断当前地址是否可以访问
function checkPermission(path) {
  // 获取所有的路径 匹配动态参数的路由会出现问题，不推荐使用，推荐使用 router.hasRoute()
  let hasPermission = router.getRoutes().filter(route => route.path.indexOf(path) > -1).length
  return hasPermission ? true : false
}

// 导航守卫
router.beforeEach((to, from, next) => {
  if (router.hasRoute(to.name)) {
    document.title = to.meta.title
    next()
  } else {
    next('/404')
  }
})

export default router